package editvois

import (
	"bytes"
	"fmt"
	"html/template"
	"net/http"

	"code.google.com/awwwe-browser/auralvm/voisfile"
	"code.google.com/awwwe-browser/data"
)

// handles the editor startup page.
func HandlerEditor(w http.ResponseWriter, r *http.Request) {
	HandlerEditorWithError(w, r, nil)
}

// handles the editor startup page.
func HandlerEditorWithError(w http.ResponseWriter, r *http.Request, err error) {
	var pdata *PageData
	var value []string
	var ok bool
	var message, editurl string
	r.ParseForm()
	value, ok = r.Form["error"]
	if ok {
		message = value[0]
	} else if err != nil {
		message = err.Error()
	} else {
		message = data.EmptyString
	}
	value, ok = r.Form["edit_url"]
	if ok {
		editurl = value[0]
	} else {
		editurl = data.EmptyString
	}
	pdata = newPageDataEditor(message, editurl)
	// page template
	t, err := template.ParseFiles(fmt.Sprintf("%s/siteLayout.tmpl", Package))
	if err == nil {
		err = t.Execute(w, pdata)
		if err != nil {
			errorOut(w, err)
		}
	}
}

// returns the view data for the editor startup page.
func newPageDataEditor(message, editurl string) *PageData {
	var pdata *PageData
	var n, v string
	var t *template.Template
	var err error
	var bfr *bytes.Buffer
	var nvpairs map[string]string
	pdata = newPageData(data.EmptyString, "editor")
	pdata.PageTitle = "VOIS Markup Editor"
	pdata.Layout = "_SiteLayout.tmpl"
	pdata.PageClass = "sitePage"
	pdata.HeaderClass = "awwwebrowsersite-header-home"
	pdata.BodyClass = "awwwebrowsersite-home"
	pdata.MainContentClass = "articleContent"
	// css urls
	pdata.CSSURL = template.URL(fmt.Sprintf(homeCssUrl, HomeURL))
	pdata.GeneralCSSURL = template.URL(fmt.Sprintf(generalCssUrl, HomeURL))
	pdata.NavCSSURL = template.URL(fmt.Sprintf(navCssUrl, HomeURL))
	pdata.FormCSSURL = template.URL(fmt.Sprintf(normalFormCssUrl, HomeURL))
	// link classe
	pdata.VoisEditorLinkClass += " currentPageLink"
	// error message
	pdata.ErrorMessage = template.HTML(message)
	// edit url
	pdata.EditURL = template.HTMLAttr(editurl)
	// javascript
	pdata.VoisEditorOnClick = currentOnClick
	// styles
	pdata.StyleFunc = notHomeStyleFunc
	// head markup
	t, err = template.ParseFiles(fmt.Sprintf("%s/head.tmpl", Package))
	if err == nil {
		bfr = bytes.NewBuffer(data.EmptyBytes)
		err = t.Execute(bfr, pdata)
		if err == nil {
			pdata.HeadMarkup = template.HTML(bfr.String())
		}
	}
	// body markup
	t, err = template.ParseFiles(fmt.Sprintf("%s/editor.tmpl", Package))
	if err == nil {
		nvpairs = voisfile.GetVOISFileNameURLMap()
		for n, v = range nvpairs {
			pdata.NameValuePairs[template.HTMLAttr(n)] = template.HTMLAttr(v)
		}
		bfr = bytes.NewBuffer(data.EmptyBytes)
		err = t.Execute(bfr, pdata)
		if err == nil {
			pdata.BodyMarkup = template.HTML(bfr.String())
		}
	}
	// navigation markup
	t, err = template.ParseFiles(fmt.Sprintf("%s/navigation.tmpl", Package))
	if err == nil {
		bfr = bytes.NewBuffer(data.EmptyBytes)
		err = t.Execute(bfr, pdata)
		if err == nil {
			pdata.NavigationMarkup = template.HTML(bfr.String())
		}
	}
	return pdata
}
